<?php
/*
 * @Description    : 喵付通异步通知
 * @Version        : 1.0.0
 * @Author         : QianLong
 * @Date           : 2022-03-16 14:50:04
 * @LastEditors    : QianLong
 * @LastEditTime   : 2024-01-16 08:51:23
 * @HomeUrl        : http://ecmpay.cn
 */

namespace app\api\controller;

use app\BaseController;
use app\common\model\Manager;
use app\common\model\Money;
use app\common\RedisCache;
use Ecmpay\Ecmpay\Ecmpay;
use think\facade\Db;

class EcmpayNotify extends BaseController
{
  /**
   * 版本升级异步通知
   * @param $data
   * @return mixed
   */
  public function version()
  {
    if (request()->isPost()) {
      $get_info = input('param.');
      if (!empty($get_info)) {
        $log_id = $get_info['trade_no'];
        $order_info = Db::connect('mongodb')->name('dev_version_pay')->where('_id', $log_id)->find();
        if (empty($order_info)) {
          throw new \Exception('无此订单');
        }
        Ecmpay::init(['app_id'=>'4681156491864621','auth_app_id'=>'2016031701220293','appSecret'=>'632d9b0d7556ce296f33035c6b3dcbe8dfa53e65b47f97a8e7d9ccf8ccdf14df']);
        if (!Ecmpay::signVerify($get_info)) {
          throw new \Exception('签名校验失败');
        }
        $trade_status = $get_info['event'];
        if ($trade_status == 'PAYMENT_SUCCESS') {
          if ($order_info['status'] !== 1) {
            Db::connect('mongodb')->name('dev_version_pay')->where('_id', $order_info['_id'])->update(['status' => 1, 'platform_trade_no' => $get_info['platform_trade_no']]);
            $merchantInfo = Manager::where('id', $order_info['uid'])->field('version,version_end_time')->find();
            $time = time();
            if ($merchantInfo['version'] == 1 || $merchantInfo['version_end_time'] < $time) {
              $update['version'] = $order_info['version_id'];
              $update['version_end_time'] = strtotime('+1 year',$time);
            }else{
              $update['version_end_time'] = strtotime('+1 year',$merchantInfo['version_end_time']);
            }
            Manager::where('id', $order_info['uid'])->update($update);
            $opData['uid'] = (int) $order_info['id'];
            $opData['username'] = $order_info['username'];
            $opData['ext_log'] = $order_info['title'].'(支付成功)，单号：'.$order_info['_id'];
            $opData['opstatus'] = 1;
            addOpLog($opData);
            $hasInviter = Manager::field('invite_uid,create_time')->where('id', $order_info['uid'])->find();
            if (!empty($hasInviter['invite_uid']) && (($hasInviter['create_time']+5184000) > time())) {
              $setting = (new RedisCache())->getSysSetting();
              InviterFxMoney('会员充值返现', $hasInviter['invite_uid'], $order_info['money'], round($order_info['money']* $setting['invite_fx_bili']/100,2), $order_info['username'], '+');
            }
            Db::connect('mongodb')->name('recharge_order')->where('_id', $order_info['_id'])->update(['status' => 1, 'success_time' => time(), 'platform_trade_no' => $get_info['platform_trade_no']]);
            $pushData['msg_type'] = 'text';
            $pushData['msg_content'] = '【开发平台】' . $order_info['username'] . ' 充值了 ￥' . $order_info['money'];
            $pushData['content'] = $pushData['msg_content'];
            $pushData['title'] = '会员充值通知';
            doSendPush($pushData);
          }
        }
        echo "success";
      } else {
        echo "empty";
      }
    }
  }
  public function recharge()
  {
    if (request()->isPost()) {
      $get_info = input('param.');
      if (!empty($get_info)) {
        $log_id = $get_info['trade_no'];
        $order_info = Db::connect('mongodb')->name('recharge_order')->where('_id', $log_id)->find();
        if (empty($order_info)) {
          throw new \Exception('无此订单');
        }
        Ecmpay::init(['app_id'=>'4681156491864621','auth_app_id'=>'2016031701220293','appSecret'=>'632d9b0d7556ce296f33035c6b3dcbe8dfa53e65b47f97a8e7d9ccf8ccdf14df']);
        if (!Ecmpay::signVerify($get_info)) {
          throw new \Exception('签名校验失败');
        }
        $trade_status = $get_info['event'];
        if ($trade_status == 'PAYMENT_SUCCESS') {
          if ($order_info['status'] !== 1) {
            Db::connect('mongodb')->name('recharge_order')->where('_id', $order_info['_id'])->update(['status' => 1, 'success_time' => time(), 'platform_trade_no' => $get_info['platform_trade_no']]);
            $hasMoneySetting = Money::field('money')->where('uid',$order_info['uid'])->find();
            if (empty($hasMoneySetting)) {
              $moneyData['uid'] = $order_info['uid'];
              $moneyData['username'] = $order_info['username'];
              $moneyData['money'] = 0;
              $moneyData['total_money'] = 0;
              Money::insert($moneyData);
            }
            Money::where('uid',$order_info['uid'])->inc('money', $order_info['money'])->inc('total_money', $order_info['money'])->update();
            $opData['uid'] = (int) $order_info['id'];
            $opData['username'] = $order_info['username'];
            $opData['ext_log'] = $order_info['title'].'(支付成功)，单号：'.$order_info['_id'];
            $opData['opstatus'] = 1;
            addOpLog($opData);
            $pushData['msg_type'] = 'text';
            $pushData['msg_content'] = '【开发平台】' . $order_info['username'] . ' 充值了 ￥' . $order_info['money'];
            $pushData['content'] = $pushData['msg_content'];
            $pushData['title'] = '会员充值通知';
            doSendPush($pushData);
          }
        }
        echo "success";
      } else {
        echo "empty";
      }
    }
  }
  //  Db::connect('mongodb')->name('contract_orders')->insert($orderData);
  public function contract()
  {
    if (request()->isPost()) {
      $get_info = input('param.');
      if (!empty($get_info)) {
        $log_id = $get_info['trade_no'];
        $order_info = Db::connect('mongodb')->name('contract_orders')->where('_id', $log_id)->find();
        if (empty($order_info)) {
          throw new \Exception('无此订单');
        }
        Ecmpay::init(['app_id'=>'4681156491864621','auth_app_id'=>'2016031701220293','appSecret'=>'632d9b0d7556ce296f33035c6b3dcbe8dfa53e65b47f97a8e7d9ccf8ccdf14df']);
        if (!Ecmpay::signVerify($get_info)) {
          throw new \Exception('签名校验失败');
        }
        $trade_status = $get_info['event'];
        if ($trade_status == 'PAYMENT_SUCCESS') {
          if ($order_info['status'] !== 1) {
            Db::connect('mongodb')->name('contract_orders')->where('_id', $order_info['_id'])->update(['status' => 1, 'platform_trade_no' => $get_info['platform_trade_no']]);
            $opData['uid'] = (int) $order_info['id'];
            $opData['username'] = $order_info['username'];
            $opData['ext_log'] = $order_info['title'].'(支付成功)，单号：'.$order_info['_id'];
            $opData['opstatus'] = 1;
            addOpLog($opData);
            Db::connect('mongodb')->name('recharge_order')->where('_id', $order_info['_id'])->update(['status' => 1, 'success_time' => time(), 'platform_trade_no' => $get_info['platform_trade_no']]);
            $pushData['msg_type'] = 'text';
            $pushData['msg_content'] = '【开发平台】' . $order_info['username'] . ' 购买合同包花费 ￥' . $order_info['money'];
            $pushData['content'] = $pushData['msg_content'];
            $pushData['title'] = '会员充值通知';
            doSendPush($pushData);
          }
        }
        echo "success";
      } else {
        echo "empty";
      }
    }
  }
}
